import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mybase/base/tool/color/FFColor.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/tool/extension/FFColorExt.dart';
import 'package:mybase/base/tool/pprint/FFLog.dart';
import 'package:mybase/base/tool/sputils/FFSpUtils.dart';

/**
 * 如果某个变量可为空，使用这种方式定义 var bgColor2 = Rxn<Color>();
 * 禁止使用 var sss = null.obs; 因为不好确定类型
 */

class FFGetx {
  static initAndEver<T>({
    required Object controller,
    required Rx variable,
    required String variableStr,
    required dynamic defaultValue,
    Function(dynamic value)? onInit,
    Function(dynamic value)? onChange,
    Function(String spKey, dynamic defaultValue)? customInit,
    Function(String spKey, dynamic value)? customChange,
  }) {
    assert(!FFEmpty.stringIsEmpty(variableStr), "变量名不能为空");
    String spStr = "${controller.runtimeType.toString()}.$variableStr";
    var typeStr = variable.value.runtimeType.toString().toLowerCase();

    switch (typeStr) {
      case "int":
        {
          variable.value = FFSpUtils.getIntData(spStr) ?? defaultValue;
          onInit?.call(variable.value);
          ever(variable, (callback) {
            FFSpUtils.saveIntData(spStr, callback);
            // FFLog.success("$spStr 更新为 ${callback}");
            onChange?.call(callback);
          });
          break;
        }

      case "string":
        {
          variable.value = FFSpUtils.getData(spStr) ?? defaultValue;
          onInit?.call(variable.value);
          ever(variable, (callback) {
            FFSpUtils.saveData(spStr, callback);
            // FFLog.success("$spStr 更新为 ${callback}");
            onChange?.call(callback);
          });
          break;
        }
      case "bool":
        {
          variable.value = FFSpUtils.getBoolNullData(spStr) ?? defaultValue;
          onInit?.call(variable.value);
          ever(variable, (callback) {
            FFSpUtils.saveBoolData(spStr, callback);
            // FFLog.success("$spStr 更新为 ${callback}");
            onChange?.call(callback);
          });
          break;
        }
      case "double":
        {
          variable.value = FFSpUtils.getDoubleData(spStr) ?? defaultValue;
          onInit?.call(variable.value);
          ever(variable, (callback) {
            FFSpUtils.saveDoubleData(spStr, callback);
            // FFLog.success("$spStr 更新为 ${callback}");
            onChange?.call(callback);
          });
          break;
        }
      case "color":
        {
          if (defaultValue is Color) {
            variable.value = hex(FFSpUtils.getData(spStr) ?? defaultValue.toHexStr);
            onInit?.call(variable.value);
            ever(variable, (callback) {
              if (variable.value is Color) {
                Color color = variable.value;
                FFSpUtils.saveData(spStr, color.toHexStr);
                // FFLog.success("$spStr 更新为 ${color.toHexStr}");
              }
              onChange?.call(callback);
            });
          } else {
            assert(false, "默认值类型不对，应该是Color才对");
          }

          break;
        }

      case "null":
        {
          assert(false, "可空类型，请自己实现");
          break;
        }

      default:
        {
          if (customInit != null && customChange != null) {
            customInit.call(spStr, defaultValue);
            ever(variable, (callback) {
              customChange.call(spStr, callback);
            });
            return;
          }

          assert(false, "initAndEver 这个<${variable.value.runtimeType.toString()}>类型未提供初始化和监听");
        }
    }

    FFLog.info("$spStr 初始化为 ${variable.value}");
  }
}
